package ar.uade.edu.fain.ad.client.web.servlet.salon.agregadoComanda;

import ar.uade.edu.fain.ad.client.web.servlet.salon.SalonServlet;
import ar.uade.edu.fain.ad.common.exception.ProductoNoDisponibleException;
import ar.uade.edu.fain.ad.common.transfer.ComandaVO;
import ar.uade.edu.fain.ad.common.transfer.PedidoVO;
import ar.uade.edu.fain.ad.common.transfer.PlatoVO;
import ar.uade.edu.fain.ad.common.transfer.SalonVO;

import java.io.IOException;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class AgregadoComandaServlet
 */
public class AgregadoComandaServlet extends SalonServlet {
	
	private static final long serialVersionUID = 1L;

	/**
	 * @see SalonServlet#SalonServlet()
	 */
	public AgregadoComandaServlet() {
		super();
	}

	@Override
	protected void doGet(
			HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		final Long salonId = (Long) 
				request.getSession().getAttribute("salonId");
		final SalonVO salonVO = new SalonVO();
		salonVO.setId(salonId);
		final Set<PedidoVO> pedidos = 
				getSalonServiceDelegate().buscarPedidosActivos(salonVO);
		request.setAttribute("pedidos", pedidos);
		forward(getViewBasePath() + "agregadoComanda/agregarComanda.jsp", 
				request, response);
	}
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(
			HttpServletRequest request,
			HttpServletResponse response) 
			throws ServletException, IOException {
		final String pedidoId = request.getParameter("pedidoId").toString();
		final String itemId = request.getParameter("itemId").toString();
		final String cantidad = request.getParameter("cantidad").toString();
		final PedidoVO pedidoVO = new PedidoVO();
		pedidoVO.setId(Long.valueOf(pedidoId));
		final ComandaVO comandaVO = new ComandaVO();
		comandaVO.setCantidad(Integer.valueOf(cantidad));
		comandaVO.setPlato(new PlatoVO());
		comandaVO.getPlato().setId(Long.valueOf(itemId));
		try {
			getSalonServiceDelegate().agregarComanda(pedidoVO, comandaVO);
			forward(getViewBasePath() + "agregadoComanda/comandaAgregada.jsp", 
					request, response);
		} catch (ProductoNoDisponibleException e) {
			request.setAttribute("producto", e.getProductoVO());
			forward(getViewBasePath() 
					+ "agregadoComanda/productoNoDisponible.jsp", 
					request, response);
		}
	}

}
